<template>
  <div class="admin-header">
    <div class="header-left">
      <button class="menu-toggle" @click="toggleSidebar"><el-icon><Menu /></el-icon></button>
      <h1>{{ pageTitle }}</h1>
    </div>
    <div class="header-right">
      <el-dropdown>
        <span class="user-info">
          <el-avatar size="small" src="https://picsum.photos/100/100?random=2"></el-avatar>
          <span>{{ adminInfo.name }}</span>
          <el-icon class="el-icon--right"><ArrowDown /></el-icon>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item @click="handleProfile">个人信息</el-dropdown-item>
            <el-dropdown-item @click="handleLogout" divided>退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Menu, ArrowDown } from '@element-plus/icons-vue'

const props = defineProps({
  pageTitle: {
    type: String,
    default: '管理控制台'
  },
  sidebarCollapsed: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['toggle-sidebar', 'logout'])

const router = useRouter()

// 获取管理员信息
const adminInfo = computed(() => {
  const info = localStorage.getItem('adminInfo')
  if (info) {
    return JSON.parse(info)
  }
  // 为了开发测试，提供一个完整的默认管理员信息
  return { 
    name: '管理员', 
    username: 'admin', // 添加username属性，避免登录检查失败
    isSuperAdmin: true 
  }
})

// 切换侧边栏
const toggleSidebar = () => {
  emit('toggle-sidebar')
}

// 处理个人信息
const handleProfile = () => {
  ElMessage.info('个人信息功能开发中')
}

// 处理退出登录
const handleLogout = () => {
  // 清除登录状态
  localStorage.removeItem('adminToken')
  localStorage.removeItem('adminInfo')
  
  // 提示退出成功
  ElMessage.success('退出登录成功')
  
  // 跳转到管理员登录页面
  router.push('/admin/login')
}
</script>

<style scoped>
.admin-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 24px;
  height: 60px;
  background-color: #fff;
  border-bottom: 1px solid #f0f0f0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.menu-toggle {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #606266;
  padding: 4px;
  border-radius: 4px;
  transition: all 0.3s;
}

.menu-toggle:hover {
  background-color: #f5f7fa;
  color: #303133;
}

.header-left h1 {
  margin: 0;
  font-size: 20px;
  color: #303133;
  font-weight: 500;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-info:hover {
  background-color: #f5f7fa;
}

.user-info span {
  color: #606266;
  font-size: 14px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .admin-header {
    padding: 0 16px;
  }
  
  .header-left h1 {
    font-size: 18px;
  }
  
  .user-info span {
    display: none;
  }
}
</style>